package study.nababy.recyclerview.first.normal.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import study.nababy.recyclerview.R;
import study.nababy.recyclerview.first.normal.adapter.StaggeredHomeAdapter;
import study.nababy.recyclerview.first.normal.derication.DividerGridItemDecoration;

public class ListRecycleActivity extends AppCompatActivity {

    private RecyclerView list_recycle_activity;
    private ArrayList<String> mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_recycle);
        initViews();
    }

    /**
     * 初始化当前页面
     */
    private void initViews() {
        //创建数据
        mDatas = new ArrayList<>();
        for (int i = 'A'; i < 'z'; i++) {
//            for (int j = 0; j < 20; j++) {
                mDatas.add((char)i + "");
//            }
        }
        list_recycle_activity = (RecyclerView) findViewById(R.id.list_recycle_activity);
//        // 如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
//        list_recycle_activity.setHasFixedSize(true);
//        // 确定每个item的排列方式
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this) {
//            @Override
//            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
//                // 这里要复写一下，因为默认宽高都是wrap_content
//                // 这个不复写，你点击的背景色就只充满你的内容
//                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            }
//        };
//
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2) {
//            @Override
//            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
//                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            }
//        };

//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        list_recycle_activity.setLayoutManager(new GridLayoutManager(this,4));
//        list_recycle_activity.setLayoutManager(new LinearLayoutManager(this));
        list_recycle_activity.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
//        MyBaseAdapter baseAdapter = new MyBaseAdapter();
        //给RecyclerView添加分割线
        StaggeredHomeAdapter baseAdapter = new StaggeredHomeAdapter(this,mDatas);
        list_recycle_activity.addItemDecoration(new DividerGridItemDecoration(this));
        list_recycle_activity.setAdapter(baseAdapter);

        baseAdapter.setOnItemClickLitener(new StaggeredHomeAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(ListRecycleActivity.this, "点击", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(ListRecycleActivity.this, "长按", Toast.LENGTH_SHORT).show();
            }
        });

    }

    class MyBaseAdapter extends RecyclerView.Adapter<MyBaseAdapter.MyViewHolder> {


        private MyViewHolder viewHolder;

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //加载自条目布局
            View view = LayoutInflater.from(ListRecycleActivity.this).inflate(R.layout.list_items, parent, false);
            viewHolder = new MyViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {

            //向子条目的TextView中添加数据
            viewHolder.list_item_texts.setText(mDatas.get(position));

        }

        @Override
        public int getItemCount() {
            //判断mDatas是否为空，避免出现空指针
            int count = mDatas == null ? 0 : mDatas.size();
            return count;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            TextView list_item_texts;

            public MyViewHolder(View itemView) {
                super(itemView);

                //查找自条目控件
                list_item_texts = (TextView) itemView.findViewById(R.id.list_item_textsm);

            }
        }

    }

}
